<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.phone"
        placeholder="请输入手机号"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.name"
        placeholder="请输入代理商名称"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-date-picker
        v-model="listQuery.startDay"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetime"
        placeholder="选择开始日期时间"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-date-picker
        v-model="listQuery.endDay"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetime"
        placeholder="选择结束日期时间"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate(2)"
      >
        新增代理商
      </el-button>
    </div>

    <el-table
      :key="key"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        prop="id"
        align="center"
        width="50"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="负责人"
        align="center"
        width=""
      />
      <el
        <el-table-column
        prop="phone"
        label="手机号码"
        align="center"
        width=""
      />
      <el-table-column
        prop="company"
        label="公司名称"
        align="center"
        width=""
      />
      <el-table-column
        label="地址"
        min-width="200"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.provinceName + scope.row.cityName + scope.row.areaName + scope.row.address }}
        </template>
      </el-table-column>
      <el-table-column
        prop="created"
        label="创建时间"
        align="center"
        width="150"
      />

      <!-- <el-table-column
        label="创建时间"
        width=""
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.created | parseTime('{y}-{m}-{d} {h}:{i}') }}
        </template>
      </el-table-column> -->

      <!-- <el
        <el-table-column
        prop="currentMoney"
        label="钱包余额"
        align="center"
        width=""
      /> -->

      <el-table-column
        label="操作"
        align="center"
        width="170"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>

          <el-button
            type="danger"
            size="mini"
            @click="detail(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      top="2vh"
      width="680px"
    >
      <el-form
        ref="temp"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="110px"
        style="width: 550px; margin-left:50px;"
      >
        <el-form-item
          label="负责人"
          prop="name"
        >
          <el-input v-model="temp.name" />
        </el-form-item>

        <el-form-item
          label="联系电话"
          prop="phone"
        >
          <el-input
            v-model="temp.phone"
            :disabled="editPhone"
          />
        </el-form-item>

        <!-- <el-form-item
          label="地址"
          prop=""
        > -->
        <provinceData
          ref="provinceData"
          :temp="temp"
          @listen="getProvince"
          @ifValid="ifValid"
        />
        <!-- </el-form-item> -->
        <!-- <el-form-item
          prop="address"
          label="详细地址"
        >
          <el-input
            v-model="temp.address"
            placeholder="详细地址"
            :value="temp.address"
            prop="address"
          />
        </el-form-item> -->

        <el-form-item
          label="公司名称"
          prop="company"
        >
          <el-input v-model="temp.company" />
        </el-form-item>

        <el-form-item
          label="营业执照"
          prop="businessLicense"
        >
          <el-input
            v-model="temp.businessLicense"
            style="display:none"
          />
          <addImgall
            :image-url="temp.businessLicense"
            @imgUrl="getImg"
          />
        </el-form-item>

        <!-- <el-form-item
          label="银行卡账号"
          prop=""
        >
          <el-input v-model="temp.bankCard" />
        </el-form-item>
        <el-form-item
          label="开户人姓名"
          prop=""
        >
          <el-input v-model="temp.bankAcountName" />
        </el-form-item> -->

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button
          type="primary"
          @click="createData()"
        >
          {{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { user_drivers } from '@/api/user'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { parseTime } from '@/utils'
import addImgall from '@/components/img'
import provinceData from '@/components/province'

export default {
  directives: { waves },
  components: { Pagination, addImgall, provinceData },
  data() {
    return {
      editPhone: true,
      province1: '',
      imageUrl: '',
      tableData: [],
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      cooperation: [],
      listQuery: {
        page: 1,
        limit: 20,
        sort: '+id',
        phone: '',
        name: '',
        startDay: '',
        endDay: ''
      },
      key: 1, // table key
      dialogFormVisible: false,
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],

        businessLicense: [
          { required: true, message: '请上传图片', trigger: 'change' }
        ],
        phone: [{ required: true, message: '请输入电话', trigger: 'blur' }],
        legalPerson: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        company: [
          { required: true, message: '请输入公司名称', trigger: 'blur' }
        ],
        address: [{ required: true, message: '请输入地址', trigger: 'blur' }]
      },
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      temp: {
        businessLicense: '',
        name: '',
        phone: '',
        address: '',
        legalPerson: '',
        provinceCode: '',
        cityCode: '',
        areaCode: ''
      }
    }
  },
  watch: {
    checkboxVal(valArr) {
      console.log('watch')

      this.formThead = this.formTheadOptions.filter(
        i => valArr.indexOf(i) >= 0
      )
      this.key = this.key + 1 // 为了保证table 每次都会重渲 In order to ensure the table will be re-rendered each time
    }
  },

  created() {
    const id = this.$route.query.id
    this.listQuery.cooperation = id
    console.log(id)
    this.getList()
  },
  methods: {
    detail(row) {
      console.log(row)
      this.$router.push({
        path: '/bankCard',
        query: {
          relationId: row.id,
          relationType: 1,
          currentMoney: row.currentMoney,
          phone: row.phone,
          name: row.name,
          type: 1 // 1是省代  2是商户
        }
      })
    },
    getImg(data) {
      console.log(data)
      this.temp.businessLicense = data
      this.imageUrl = data
    },
    getProvince(data) {
      console.log('0000000000000000000000')
      console.log(data)
      this.temp.provinceCode = data.provinceCode
      this.temp.cityCode = data.cityCode
      this.temp.areaCode = data.areaCode
      this.temp.address = data.address
    },
    resetTemp() {
      this.temp = {
        businessLicense: '',
        name: '',
        phone: '',
        address: '',
        legalPerson: '',
        provinceCode: '',
        cityCode: '',
        areaCode: ''
      }
    },
    handleCreate(role) {
      this.resetTemp()
      this.temp.role = role
      console.log(this.temp)
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.editPhone = false

      this.$nextTick(() => {
        this.$refs['temp'].clearValidate()
      })
      this.$refs.provinceData.clearProvinceDate()
    },
    ifValid(ifValid) {
      if (ifValid) {
        this.$refs['temp'].validate(valid => {
          if (valid) {
            console.log(this.temp)
            createProvince(this.temp).then(res => {
              // this.list.unshift(this.temp);
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: res.msg,
                type: 'success',
                duration: 2000
              })
              this.getList()
            })
          }
        })
      }
    },
    createData() {
      this.$refs.provinceData.submitForm()
      this.$refs['temp'].validate(valid => {
        return valid
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      console.log(this.temp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$refs.provinceData.getCityData()
      this.editPhone = true
      this.$nextTick(() => {
        this.$refs['temp'].clearValidate()
      })
      this.$refs.provinceData.clearProvinceDate()
    },
    getList() {
      this.listLoading = true
      user_drivers(this.listQuery).then(res => {
        this.tableData = res.map.userPage.content
        console.log( this.tableData)
        this.total = res.map.userPage.totalPages
        console.log(this.tableData)
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    getCooperationAll() {
      cooperationAll().then(res => {
        this.cooperation = res.data

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    }
  }
}
</script>

